from .functions import *

class ResponseDataPackage(object):
	"""docstring for ResponseDataPackage"""
	def __init__(self, 
			sid = 0,
			did = 0,
			rcm = 0,
			ret = 0,
			data = '',
			packageString = ''):
		super(ResponseDataPackage, self).__init__()
		if (len(packageString) > 0):
			if not checkSum(packageString) : raise Exception("Checksum validation not valid!\r\nComposing data: " + packageString)
			self.sid = parseByte(packageString[2 * 2 : 3 * 2])
			self.did = parseByte(packageString[3 * 2 : 4 * 2])
			self.rcm = parseWord(packageString[4 * 2 : 6 * 2])
			self.len = min(parseWord(packageString[6 * 2 : 8 * 2]), 500)
			self.ret = parseWord(packageString[8 * 2 : 10 * 2])
			self.data = packageString[10 * 2 : 10 * 2 + (self.len - 2) * 2]
		else:
			self.sid = sid
			self.did = did
			self.rcm = rcm
			self.len = min(len(data) / 2 + 2, 500)
			self.ret = ret
			self.data = data[0 : (self.len - 2) * 2]

	def getPackageString(self):
		prefix = 'A55A'
		sid = makeByte(self.sid)
		did = makeByte(self.did)

		data = self.data

		pkgData = prefix + sid + did + makeWord(self.rcm) + makeWord(self.len) + makeWord(self.ret) + data

		pkgData = pkgData + calcChkSum(pkgData)
		return pkgData

	def getPackageData(self):
		return binascii.unhexlify(self.getPackageString())